<?php
class player
{
	var $media,$n,$info,$album,$m,$mark,$id,$artobj;		
	function song($id)
	{
		$sql="SELECT * FROM `media` WHERE `media_id`='$id'";
		$info=mysql_fetch_assoc(mysql_query($sql));
		$this->id=$id;
		$this->intro_song($info);
		$this->playsong($id,613,354);
		$this->reload($id);
		$this->toolbar();
		$this->lyric();
		$this->artist();
		$this->relative();
		$this->list_album();
		$this->list_video();
		$this->comment($id);
		$this->list_comment($id);
	}
	
	function album($id)
	{	
	 	$cmd="SELECT * FROM `album` WHERE `album_id`='$id'";
		$ab=mysql_fetch_assoc(mysql_query($cmd));
		$sql="UPDATE `album` SET album_view=".($ab['album_view']+1)." WHERE album_id=".$ab['album_id'];
		mysql_query($sql);		
		$sql="SELECT * FROM `media` WHERE `media_album`='$id' ORDER BY media_order ASC";
		$excute=mysql_query($sql); $dem=0;
		while ($info=mysql_fetch_array($excute,MYSQL_ASSOC))
		$mang[++$dem]=$info;
		$this->media=$mang; $this->n=$dem;
		$this->intro_album($ab);
		$this->listmedia($id);
		$this->comment($id);
		$this->list_comment($id);		
	}
	
	
	
	function listmedia()
	{	
	echo '<div class="player_swf"><div class="player" id="load_player">';
		$n=$this->n;	
		$media=$this->media;
for ($i=1;$i<=$n-1;$i++) $nextsong[$media[$i]['media_id']]=$nextsong[$media[$i+1]['media_id']];
		$nextsong[$media[$n]['media_id']]=$nextsong[$media[1]['media_id']];
		echo'<div id="player">';
		$this->playalbum($media[1]['media_id'],613,354); 
		echo '</div>';
		echo '<div id="player">';
		if ($n>5) echo'<div class="playlist_more">'; else echo'<div class="playlist">';
		echo '<ul>';		
		for ($i=1;$i<=$n;$i++) 
		{ echo '<li><a class="items" song="'.$media[$i]['media_hash'].'">'.$i.' . '.$media[$i]['media_name'].'</a><a class="singer"> - '.decode2($media[$i]['media_artist'],2).'</a></li>';
		}
		echo '</ul></div></div>';
		echo "</div></div>";
		$this->toolbar();
		echo '<div id="info">';
		echo "<script>
			$(document).ready(function(){
				$('.one').css('font-weight','bold');
			});
		</script>";	
		echo '</div>';
	}
	
	function intro_song($info){
		$cmd="SELECT * FROM `cat` WHERE `cat_id`=".$info['media_cat'];
		$cat=mysql_fetch_assoc(mysql_query($cmd));
		sql2();
		$cmd="SELECT * FROM `user` WHERE `userid`=".$info['media_poster'];
		$user=mysql_fetch_assoc(mysql_query($cmd));
		sql();
			echo '<div id="songtitle"><a class="title" href="?song='.$info['media_id'].'">'.$info['media_name'].' - '.$cat['cat_name'].'</a></div>
        Nghệ sĩ: <a class="singer">'.decode($info['media_artist']).'</a> | Soạn giả: <a class="singer">'.decode($info['media_author']).'</a><br/>
        Ðóng góp: <a class="singer">'.$user['username'].'</a> | Lượt nghe: '.$info['media_view'].' | '.($info['media_bitrate']/1000).'kb/s 
</div>';

space(2);
}
	
	function intro_album($info){
	echo'<div class="albumdetail"><table width="100%" border="0"><tr>
	<td style="vertical-align:top" width="150"><img src="';
	if (substr($info['album_img'],0,4)=='http') echo $info['album_img']; else echo 'http://beta.cailuongso.com/'.$info['album_img'];
	echo '"></td>
		<td style="vertical-align:top">
		<div class="albumtitle"><a href="?album='.$info['album_id'].'">'.$info['album_name'].'</a></div>
		<div class="albumadd">
		<b>Lượt nghe: </b>'.$info['album_view'].' <br/>
<b>Năm phát hành: </b>2012 </br>
<b>Số bài hát: </b>'.$this->n.'<br/>
<b>Thể loại: </b>Trích đoạn cải lương </br>
		</div>
		<div class="albuminfo"><div class="ex_album">'.$info['album_detail'].'</div></div></td>
		<tr>
		</table></div>			
			';
		echo'</div>';
		
	}	
	
	function playsong($id,$width,$height)
	{	
		$sql="SELECT * FROM `media` WHERE `media_id`='$id'";
		$info=mysql_fetch_assoc(mysql_query($sql));		
		$sql="UPDATE `media` SET media_view=".($info['media_view']+1)." WHERE media_id=".$info['media_id'];
		mysql_query($sql);		
		echo'<div class="player_swf"><div class="player" id="load_player"><div id="player">
		<div id="container"></div>
		
<script type="text/javascript">
	   jwplayer("container").setup({
        file: "http://beta.cailuongso.com/songembed/'.md5($info['media_id'].'memhuynh').'/'.md5(md5($info['media_id'].'memhuynh')."agbhjkliopsjweqwl").'.xml",
        flashplayer: "http://beta.cailuongso.com/media/player.swf",
        height: '.$height.',
        width: '.$width.',
		autostart:true,	
		plugins:"hd-1",
		abouttext:"Cải lương Số",
		aboutlink:"http://beta.cailuongso.com",
		dock:true,
		controlbar:"bottom",
    });
    jwplayer().onPlay(function() {setText("Video is playing!"); });
    jwplayer().onPause(function() {setText("Why did you pause?"); });
    jwplayer().onBuffer(function() {setText("Video is buffering..."); });
    jwplayer().onIdle(function() {
		id='.$info["media_id"].';
		setText("Stop...");
		 
		});
    function setText(text) {
        document.getElementById("message").innerHTML = text;
    }
	</script>
	</div>
	</div></div>
	';
}
	
	function playalbum($id,$width,$height)
	{		
		$sql="SELECT * FROM `media` WHERE `media_id`='$id'";
		$info=mysql_fetch_assoc(mysql_query($sql));		
		echo'<div id="container"></div>
		<div class="spacemedia"></div>
<script type="text/javascript">
	   jwplayer("container").setup({
        file: "http://beta.cailuongso.com/songembed/'.md5($info['media_id'].'memhuynh').'/'.md5(md5($info['media_id'].'memhuynh')."agbhjkliopsjweqwl").'.xml",
        flashplayer: "http://beta.cailuongso.com/media/player.swf",
        height: '.$height.',
        width: '.$width.',
		autostart:true,	
		plugins:"hd-1",
		abouttext:"Cải lương Số",
		aboutlink:"http://beta.cailuongso.com",
		dock:true,
		controlbar:"bottom",
    });
    jwplayer().onPlay(function() {setText("Video is playing!"); });
    jwplayer().onPause(function() {setText("Why did you pause?"); });
    jwplayer().onBuffer(function() {setText("Video is buffering..."); });
    jwplayer().onIdle(function() {
		id='.$info["media_id"].';
		setText("Stop...");
		 
		});
    function setText(text) {
        document.getElementById("message").innerHTML = text;
    }
</script> ';}
	
	function lyric()
	{
	$info=$this->info;
	echo '<div id="lyric"><div class="lyric">';
	echo '<a class="lyric_title">'.'Lời Nhạc '.$info['media_name'].'</a><br/>';	
	if ($info['media_lyric']=='') 
	{
	     echo 'Đang cập nhật lời nhạc';
	} else
	{
	echo 'Đóng góp :'.$info['media_poster'].'<br/>';
	space(1);
	echo '<div class="ex_lyric">'.$info['media_lyric'].'</div>';
	}
	echo '</div></div>';
	
	}
	
	function toolbar(){		
	if (isset($_GET['album'])) $id=dehash($_GET['album'],'album'); else $id=dehash($_GET['song'],'media'); 
	if (isset($_GET['song'])) $cmd="SELECT * FROM `media` WHERE `media_id`=".$id; else $cmd="SELECT * FROM `album` WHERE `album_id`=".$id;	
	$info=mysql_fetch_assoc(mysql_query($cmd));
	space(3);
	echo '<ul class="toolbar">';	
	if (isset($_GET['song'])) 
	{
	  // if (substr($info['media_link'],strlen($info['media_link'])-3,3)=='mp3') echo '<li class="color1"><a href="http://beta.cailuongso.com/download.php?id='.md5($id.'memhuynh').'">Tải nhạc</a></li>';
	   echo'<li class="color2"><a>Giỏ nhạc</a></li>';
	}
	   echo'<li class="color3"><a>Chia sẻ</a></li>
		<li class="color4"><a>Thích</a></li>';	
         if ((!isset($_GET['album'])) && (substr($info['media_link'],strlen($info['media_link'])-3,3)!='mp3')) echo '<li class="color5 extend"><a><div id="extendtext">Mở rộng</div></a></li>';		
	echo '</ul>
	<div class="coutlike"><br/></div>';
	space(1);
	
	if ($_GET['type']=='album') 
$embedlink='[ALBUM]http://beta.cailuongso.com/albumembed/'.md5($info['album_id'].'memhuynh').'/'.md5(md5($info['album_id'].'memhuynh')."agbhjkliopsjweqwl").'.xml[/ALBUM]';
	else
		$embedlink='[MEDIA]http://beta.cailuongso.com/songembed/'.md5($info['media_id'].'memhuynh').'/'.md5(md5($info['media_id'].'memhuynh')."agbhjkliopsjweqwl").'.xml[/MEDIA]';
	echo'<div class="mediashare">
	<table>
	<tr>
	     <td width="100px">Chia sẻ bài hát</td>
		 <td><input type="text" value="http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'"/></td>
	</tr>
	<tr>
         <td>Nhúng diễn đàn</td>
		 <td><input type="text" value="'.$embedlink.'"/></td>	
	</tr></table>';
	echo'</div>';
	}

	function reload($id)
	{
		$sql="SELECT * FROM `media` WHERE `media_id`='$id'";
		$info=mysql_fetch_assoc(mysql_query($sql));
		$this->info=$info;		
	}
	
	function artist()
	{
	$tmp=$this->info;	
	if ($tmp['media_artist']!='')
	{
	$artist=encode($tmp['media_artist']);
	if (count($artist)>0)
	{
	$n=0; foreach ($artist as $i) $n++; $this->m=$n;
	$this_artist=rand(1,$n);
	$this->mark=$this_artist;
	$id=$artist[$this_artist];	
	$sql="SELECT * FROM `select` WHERE select_id=".$id;
	$info=mysql_fetch_assoc(mysql_query($sql));
	$this->artobj=new select($info['select_hash']);
	echo '<br/><div class="article-tools clearfix"><div class="article-meta">THÔNG TIN NGHỆ SĨ</div></div>';
	space(2);
	echo '<div class="artist_detail"><table width="100%" border="0"><tr>';
		if ($info['select_img']=='') $linkimg='http://beta.cailuongso.com/data/image/noavatar.jpg';
		else $linkimg='http://beta.cailuongso.com/'.$info['select_img'];
		echo '<td width="80px" style="vertical-align:top"><img src="'.$linkimg.'"></td>
		<td style="vertical-align:top">';
		echo '<a><b>Nghệ danh : </b>'.$info['select_art'].'</a><br/>';
		echo '<a><b>Tên thật : </b>'.$info['select_name'].'</a><br/>';
		echo '<a><b>Năm sinh : </b>'.$info['select_birthday'].'</a><br/>';
		echo '<a class="artist_info"><b>Thành tích nghệ thuật : </b><div class="ex_info">'.$info['select_info'].'</div></a>';
		echo '</td>
		<tr>
	</table></div>';
	}}}	

	function relative(){
	space(2);
	$tmp=$this->info;	
	$artist=encode($tmp['media_artist']);	
	if ($artist[2]){
	$cmd="SELECT * FROM `select` WHERE select_id=".$artist[$this->mark];
	$info=mysql_fetch_assoc(mysql_query($cmd));	
	echo '<div id="listmedia" class="listmedia">
	<a class="artist_more">Nghệ sĩ diễn chung với '.$info['select_art'].'</a>';
	$this->artist=$info['select_art'];
	space(2);
	echo '<div id="slide" class="relative"><table border="0"><tr>';
	foreach ($artist as $i)
	if ($i!=$artist[$this->mark]){
        $cmd="SELECT * FROM `select` WHERE select_id=".$i;
		$info=mysql_fetch_assoc(mysql_query($cmd));
		if ($info['select_img']=='') $linkimg='http://beta.cailuongso.com/data/image/noavatar.jpg';
		else $linkimg='http://beta.cailuongso.com/'.$info['select_img'];
		echo '<td><a href="http://beta.cailuongso.com/Nghệ-sĩ/'.isolink2($info['select_art']).'/'.$info['select_hash'].'.html"><img src="'.$linkimg.'"></a></td>';
	} 
	echo '<td></td></tr><tr>';  
    foreach ($artist as $i)
	if ($i!=$artist[$this->mark]){
        $cmd="SELECT * FROM `select` WHERE select_id=".$i;
		$info=mysql_fetch_assoc(mysql_query($cmd));
		echo '<td>'.$info['select_art'].'</td>';} 	
	echo'<td></td></tr></table></div></div>';
	}

	}
	
	function list_album()
	{
	echo '
	<div id="listmedia" class="listmedia">
	<a class="artist_more">Album của '.$this->artist.'</a>';
	space(2);
	echo '</div>';	
	}
	
	function list_video(){
	echo '
	<div id="listmedia" class="listmedia">
	<a class="artist_more">Video của '.$this->artist.'</a>';
	space(2);
	$artistobj=$this->artobj; 
	$info=$artistobj->info;
	echo '<div class="artvideo">';
	echo'<div class="item">';
	$cmd="SELECT * FROM `media` WHERE media_link LIKE '%youtu%'";
	$excute=mysql_query($cmd);
	$dem=0;
	
	while ($row=mysql_fetch_array($excute,MYSQL_ASSOC))
    if (select::detect_artist($info['select_id'],$row['media_artist'])){
	$dem++;
	echo '<div class="videohot">';
	if ($dem%4!=0) echo '<div class="video12x">'; else echo '<div class="video3">'; 
	echo '
    <a title="'.$row['media_name'].'" href="http://beta.cailuongso.com/Video/'.isolink2($row['media_name']).'/'.$row['media_hash'].'.html">
    <img src="http://beta.cailuongso.com/'.$row['media_img'].'" /></a>';
	space(1);
	echo'<a class="title2" href="http://beta.cailuongso.com/Video/'.isolink2($row['media_name']).'/'.$row['media_hash'].'.html">'.readmore($row['media_name'],4).'</a><br />
    <a class="singer" title="'.decode($row['media_artist']).'">'.readmore(decode($row['media_artist']),5).'</a>
	</div></div>'; 
	}   
	echo '<div class="clr"></div>';
	
	echo '</div></div></div>';
	
	
	
 /*

*/	
 	}		
	
	function comment(){
	echo '<br/><div class="article-tools clearfix"><div class="article-meta">BÌNH LUẬN</div></div>';
if(check()){
	echo'
<div id="comment">
<div class="comment">
<form method="post" enctype="multipart/form-data" action="http://beta.cailuongso.com/comment.php">
<table border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td style="vertical-align:top">
		<img src="http://diendan.cailuongso.com/image.php?u='.$_SESSION['alogin']['id'].'"></td>
		<td style="vertical-align:top"> 
			<textarea name="comment_text"></textarea>
		</td>
	</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" class="notecomment">
	<tr>
		<td width="485px" style="vertical-align:top">
		<a>Bình luận chỉ chấp nhận có dấu tiếng Việt rõ ràng, độ dài hơn 50 ký tự !</a>
		</td>
		<td style="vertical-align:top"> 
		<div class="sendpress"><input type="submit" name="submit" value="Bình luận"></div>
		<div class="commentinfo">
		      <input name="poster" value="'.$_SESSION['alogin']['id'].'">
		     <input name="type" value="';
	    if (($_GET['type']=='audio') || ($_GET['type']=='video')) echo 'media'; else echo 'album';
			 echo '">
			 <input name="code" value="';
	    if (isset($_GET['song'])) echo $_GET['song']; else echo $_GET['album'];
	    echo '">
		</div>
		
		</td>
	</tr>
</table></form>';
space(2);
echo'
</div>
</div>';
	}}
	
	function list_comment($id)
	{
		$media=$this->media;
		space(3);
		if (isset($_GET['album'])) 
		{
		   $abm='album'; 
		   
		   $hash=$_GET['album'];
		} else 
		{
		   $abm='media';
		   $hash=$_GET['song'];
		}
		$cmd="SELECT * FROM `comment` WHERE comment_".$abm."='".dehash($hash,$abm)."' ORDER BY comment_time DESC";
		$excute=mysql_query($cmd);
		if (mysql_num_rows($excute)==0) echo 'Hiện tại chưa có ai bình luận về '.$_GET['type'].' này';
		echo '<table border="0" class="commentlist">';
		while ($info=mysql_fetch_array($excute,MYSQL_ASSOC)){			
			echo '<tr>
				<td style="vertical-align:top" width="100px"><img src="http://diendan.cailuongso.com/image.php?u='.$info['comment_poster'].'"></td>
				<td style="vertical-align:top"><div class="boxlcomment"><a>'.finduser($info['comment_poster']).'</a> đã bình luận cách đây '.changetime($info['comment_time']).'
				<div class="commentline"></div>
				'.$info['comment_text'].'
				</div>				
				</td>
			</tr>
			<tr><td>&nbsp</td></tr>';
			
		}		
		echo '</table>';
	}
}
?>